import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import 'lib-flexible/flexible'
import axios from 'axios'
import VueAxios from 'vue-axios'

import { validateTokenUrls } from '../src/assets/js/url.js'

//从vant导入组件
import {
  Tabbar,
  TabbarItem,
  Search,
  Toast,
  NavBar,
  Stepper,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Form,
  Field,
  Button,
  Popup,
  // 滑动单元格
  SwipeCell,
  // 复选框
  Checkbox,
  // 提交订单栏
  SubmitBar,
  // 空状态
  Empty,
  // tag标签
  Tag,
  // 图标
  Icon,
  AddressList,
  AddressEdit,
  PasswordInput,
  NumberKeyboard,
  Dialog,
  Tab,
  Tabs,
  Skeleton,
  Uploader,
  Swipe,
  SwipeItem
} from 'vant';
// import { format } from 'core-js/core/date'
// import { config } from 'vue/types/umd'


Vue
  .use(Tabbar)
  .use(TabbarItem)
  .use(Search)
  .use(Toast)
  .use(NavBar)
  .use(Stepper)
  .use(GoodsAction)
  .use(GoodsActionIcon)
  .use(GoodsActionButton)
  .use(Form)
  .use(Field)
  .use(Button)
  .use(Popup)
  .use(SwipeCell)
  .use(Checkbox)
  .use(SubmitBar)
  .use(Empty)
  .use(Tag)
  .use(Icon)
  .use(AddressList)
  .use(AddressEdit)
  .use(PasswordInput)
  .use(NumberKeyboard)
  .use(Dialog)
  .use(Tab)
  .use(Tabs)
  .use(Skeleton)
  .use(Uploader)
  .use(Swipe)
  .use(SwipeItem)

//设置请求路径
axios.defaults.baseURL = 'http://www.kangliuyong.com:10002'



// 添加请求拦截器
axios.interceptors.request.use(config => {
  // 在发送请求之前做些什么
  

  
  //发送请求之前，显示加载中
  Toast.loading({
    message: '加载中...',
    forbidClick: true,
    duration: 0
  });

  //判断
  let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';

  let token = sessionStorage.getItem('token');

  if (config.method === 'post') {

    //进行post请求参数序列化
    config.data = config.data || {};
    let dataString = '';
    for (let key in config.data) {
      dataString += `${key}=${config.data[key]}&`
    }

    config.data = dataString + 'appkey=' + appkey;

    //找到需要验证的token
    if (validateTokenUrls.indexOf(config.url) > -1) {
      config.data += `&tokenString=${token}`;
    }

  } else if (config.method === 'get') {
    config.params = config.params || {};
    config.params.appkey = appkey;

    //找到需要验证的token
    if (validateTokenUrls.indexOf(config.url) > -1) {
      config.params.tokenString = token
    }
  }

  return config;
}, error => {
  // 对请求错误做些什么
  Toast.clear();
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(response => {
  // 对响应数据做点什么
  //服务器响应数据后,关闭加载提示
  Toast.clear();
  return response;
}, error => {
  // 对响应错误做点什么
  return Promise.reject(error);
});

Vue.use(VueAxios, axios)

Vue.filter("formatDate",(value, format) =>{
  // value: 需要进行格式化的数据
  // format: 日期的格式 yyyy符号MM符号dd hh符号mm符号ss
  
  let currentDate = new Date(value);
  let year = currentDate.getFullYear();
  if(/(y+)/.test(format)){
    // 获取组匹配的内容
    let content = RegExp.$1;
    // 
    year = year.toString().slice(4 - content.length);
    format = format.replace(content, year);
  }
  // 

  let dateObject = {
    M: currentDate.getMonth() + 1,
    d: currentDate.getDate(),
    h: currentDate.getHours(),
    m: currentDate.getMinutes(),
    s: currentDate.getSeconds()
  };
  // 进行替换format
  for(let key in dateObject){
      // 根据key生成动态正则表达式
      let reg = new RegExp(`(${key}+)`);
      // 
      if(reg.test(format)){
        // 获取组的匹配内容
        let c = RegExp.$1;
        
        let v = dateObject[key] >= 10 ? dateObject[key] : c.length === 2 ? "0" + dateObject[key] : dateObject[key];
        format = format.replace(c,v);
        // 
      }
  }
  return format;
})

Vue.config.productionTip = false

new Vue({
  data: {
    //购物车数量0
    publicShopbagCount: ''
  },
  router,
  store,
  render: h => h(App)
}).$mount('#app')
